package com.mh.common.core.domain;

import com.mh.system.domain.SysDept;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/** 构建树形结构实体类
 *  TreeSelect 的存在是为了解耦前端数据结构与数据库实体
 *  SysDept是数据库实体,可能包含很多字段,但前端树形下拉组件只需要id、label、children
 *  TreeSelect 是专为前端设计的轻量级对象,避免暴露不必要的数据
 *  数据库字段是 deptId、deptName, 但前端组件需要 id、label
 */
public class TreeSelect implements Serializable {

    private static final long serialVersionUID = -4096339404159357730L;

    private Long id; //节点Id(deptId)
    private String label; //节点名称(deptName)
    private List<TreeSelect> children; //子节点

    //空参
    public TreeSelect() {
    }
    //全参,构建树形结构的核心
    public TreeSelect(SysDept dept) {
        this.id = dept.getDeptId();
        this.label = dept.getDeptName();
        /* 典型的构造器递归, 形成树形结构
           将List<SysDept>对象 转换为 TreeSelect(通过TreeSelect构造器)
           dept.getChildren() 返回List<SysDept>
           .map(TreeSelect::new) = .map(dept -> new TreeSelect(dept))
           构造方法引用的语法: 类名::new */
        this.children = dept.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
    }
    //set(),get()
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public List<TreeSelect> getChildren() {
        return children;
    }

    public void setChildren(List<TreeSelect> children) {
        this.children = children;
    }
}
